﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Phoenix.Backend
{
    public partial class TrackingEList : System.Web.UI.Page
    {
        protected DataTable dt;
        DataTable dtTrackingE;
        Phoenix.AppCode.Entities.TrackingEList trackingelist = new Phoenix.AppCode.Entities.TrackingEList();
        string success = "<div class='alert_success'><p><img src='../Content/Backend/images/icon_accept.png' alt='success' class='mid_align'/>" + "Cập nhật thông tin thành công." + "</p></div>";
        string error = "<div class='alert_error'><p><img src='../Content/Backend/images/icon_error.png' alt='error' class='mid_align'/>" + "Máy chủ đang bận. Vui lòng thử lại." + "</p></div><br class='clear'/>";
        string info = "<div class='alert_info'><p><img src='../Content/Backend/images/icon_info.png' alt='info' class='mid_align'/>" + "Thông tin chưa được cập nhật." + "</p></div><br  class='clear'/>";
        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    if (Session["Theme"].ToString() != "")
        //    {
        //        Page.Theme = Session["Theme"].ToString();
        //    }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    // load drop down list TrackingEList type
                    dtTrackingE = Phoenix.AppCode.Bussiness.TrackingEService.TrackingEList();

                    ddlTrackingE.DataSource = dtTrackingE;
                    ddlTrackingE.DataTextField = "NumberTrackingE";
                    ddlTrackingE.DataValueField = "ID_TrackingE";
                    ddlTrackingE.DataBind();
                    //add select dropdownlist
                    ddlTrackingE.Items.Insert(0, new ListItem("Xem tất cả", "0"));

                    if (Request.Cookies["ddlTrackingE"] == null)
                    {
                        Response.Cookies["ddlTrackingE"].Value = "0";
                        Response.Cookies["ddlTrackingE"].Expires = DateTime.Now.AddDays(1);
                    }
                    dt = Phoenix.AppCode.Bussiness.TrackingEListService.TrackingEListListById(Convert.ToInt32(Request.Cookies["ddlTrackingE"].Value.Trim()), Request.Cookies["Lang_Backend"].Value);

                    ddlTrackingE.SelectedValue = Request.Cookies["ddlTrackingE"].Value;

                    if (Request.QueryString["Action"] == "Create" || Request.QueryString["Action"] == "Edit" || Request.QueryString["Action"] == "OnOff" || Request.QueryString["Action"] == "Delete")
                    {
                        add.Visible = true;
                        edit.Visible = true;
                        Load_ActionTrackingEList(Request.QueryString["Action"]);
                    }
                }
                catch (FormatException)
                {
                    Response.Redirect("Error.aspx");
                }
            }
        }

        #region  Load action TrackingEList
        private void Load_ActionTrackingEList(string action)
        {
            //panelCreateCategory.Visible = visible;
            try
            {
                //DirectoryInfo dirLanguage = new DirectoryInfo(Server.MapPath(@"~\" + @"Languages"));
                if (action == "Create")
                {
                    add.Visible = true;
                    edit.Visible = false;
                    list.Visible = false;

                    // load drop down list TrackingEList type
                    dtTrackingE = Phoenix.AppCode.Bussiness.TrackingEService.TrackingEList();

                    ddlTrackingEA.DataSource = dtTrackingE;
                    ddlTrackingEA.DataTextField = "NumberTrackingE";
                    ddlTrackingEA.DataValueField = "ID_TrackingE";
                    ddlTrackingEA.DataBind();

                    ddlTrackingEA.SelectedValue = Request.Cookies["ddlTrackingE"].Value;
                }
                else if (action == "Edit")
                {
                    add.Visible = false;
                    edit.Visible = true;
                    list.Visible = false;

                    // load drop down list TrackingEList type
                    dtTrackingE = Phoenix.AppCode.Bussiness.TrackingEService.TrackingEList();

                    ddlTrackingEE.DataSource = dtTrackingE;
                    ddlTrackingEE.DataTextField = "NumberTrackingE";
                    ddlTrackingEE.DataValueField = "ID_TrackingE";
                    ddlTrackingEE.DataBind();

                    DataTable dt_Edit = Phoenix.AppCode.Bussiness.TrackingEListService.TrackingEListList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                    ddlTrackingEE.SelectedValue = dt_Edit.Rows[0]["ID_TrackingE"].ToString();
                    txtDateE.Text = DateTime.Parse(dt_Edit.Rows[0]["Date"].ToString()).ToString("dd/MM/yyyy");
                    txtTrackEventE_vi.Text = dt_Edit.Rows[0]["TrackEvent"].ToString();
                    txtTrackEventE_en.Text = dt_Edit.Rows[1]["TrackEvent"].ToString();
                    txtStateE_vi.Text = dt_Edit.Rows[0]["State"].ToString();
                    txtStateE_en.Text = dt_Edit.Rows[1]["State"].ToString();
                }
                else if (action == "Delete")
                {
                    try
                    {
                        trackingelist = Phoenix.AppCode.Bussiness.TrackingEListService.GetTrackingEListByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.TrackingEListService.Delete(trackingelist);
                        lblWarning.Text = success;
                        //Response.Redirect("TrackingEList.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("Error.aspx");
                    }
                }
                else if (action == "OnOff")
                {
                    try
                    {
                        trackingelist = Phoenix.AppCode.Bussiness.TrackingEListService.GetTrackingEListByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
                        Phoenix.AppCode.Bussiness.TrackingEListService.OnOff(trackingelist);
                        lblWarning.Text = success;
                        //Response.Redirect("TrackingEList.aspx");
                    }
                    catch
                    {
                        lblWarning.Text = error;
                        //Response.Redirect("Error.aspx");
                    }
                }
                else
                {
                    add.Visible = false;
                    edit.Visible = false;
                    list.Visible = true;
                }
            }
            catch
            {
            }
        }
        #endregion

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                trackingelist.ID_TrackingE = Convert.ToInt32(ddlTrackingEA.SelectedValue);
                trackingelist.Date = DateTime.Parse(txtDateA.Text); //DateTime.Now.ToString("G", CultureInfo.CreateSpecificCulture("en-US"))
                trackingelist.TrackEvent_vi = txtTrackEventA_vi.Text;
                trackingelist.TrackEvent_en = txtTrackEventA_en.Text;
                trackingelist.State_vi = txtStateA_vi.Text;
                trackingelist.State_en = txtStateA_en.Text;
                trackingelist.PostUser = Session["UserName"].ToString();
                Phoenix.AppCode.Bussiness.TrackingEListService.Create(trackingelist);

                ClearAllFields();
                lblWarningA.Text = success;
                //Response.Redirect("TrackingEList.aspx?Action=Create");
            }
            catch
            {
                lblWarningA.Text = error;
                //Response.Redirect("Error.aspx");
            }
        }
        protected void btnCancelA_Click(object sender, EventArgs e)
        {
            ClearAllFields();
            lblWarningA.Text = info;
        }

        private void ClearAllFields()
        {

            txtDateA.Text = "";
            txtTrackEventA_vi.Text = "";
            txtTrackEventA_en.Text = "";
            txtStateE_vi.Text = "";
            txtStateE_en.Text = "";

        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {

            trackingelist = Phoenix.AppCode.Bussiness.TrackingEListService.GetTrackingEListByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));

            trackingelist.ID_TrackingE = Convert.ToInt32(ddlTrackingEE.SelectedValue);
            trackingelist.Date = DateTime.Parse(txtDateE.Text); 
            trackingelist.TrackEvent_vi = txtTrackEventE_vi.Text;
            trackingelist.TrackEvent_en = txtTrackEventE_en.Text;
            trackingelist.State_vi = txtStateE_vi.Text;
            trackingelist.State_en = txtStateE_en.Text;
            trackingelist.EditUser = Session["UserName"].ToString();

            Phoenix.AppCode.Bussiness.TrackingEListService.Edit(trackingelist);
            lblWarningE.Text = success;
            //Response.Redirect("TrackingEList.aspx?Action=Edit&ID=" + Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()) + "");

        }
        protected void btnCancelE_Click(object sender, EventArgs e)
        {
            DataTable dt_Edit = Phoenix.AppCode.Bussiness.TrackingEListService.TrackingEListList_GetByID(Convert.ToInt32(Request.QueryString["ID"].ToString().Trim()));
            ddlTrackingEE.SelectedValue = dt_Edit.Rows[0]["ID_TrackingE"].ToString();
            txtDateE.Text = DateTime.Parse(dt_Edit.Rows[0]["Date"].ToString()).ToString("dd/MM/yyyy");
            txtTrackEventE_vi.Text = dt_Edit.Rows[0]["TrackEvent"].ToString();
            txtTrackEventE_en.Text = dt_Edit.Rows[1]["TrackEvent"].ToString();
            txtStateE_vi.Text = dt_Edit.Rows[0]["State"].ToString();
            txtStateE_en.Text = dt_Edit.Rows[1]["State"].ToString();
            lblWarningE.Text = info;
        }

        protected void ddlTrackingE_SelectedIndexChanged(object sender, EventArgs e)
        {
            dt = Phoenix.AppCode.Bussiness.TrackingEListService.TrackingEListListById(Convert.ToInt32(ddlTrackingE.SelectedValue), Request.Cookies["Lang_Backend"].Value);
            Response.Cookies["ddlTrackingE"].Value = ddlTrackingE.SelectedValue;
        }
    }
}